package com.neu.wms.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.wms.util.RedisCache;
import com.neu.wms.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author DAIJUNQIANG
 * @version 1.0
 * @description TODO
 * @date 2023/7/4 15:32
 */
@Component
public class JWTLogoutSuccessHandler  implements LogoutSuccessHandler {

	@Autowired
	private RedisCache redisCache;

	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
		redisCache.deleteObject("neu:" + ((LoginUser) authentication.getPrincipal()).getUsername());
		response.setContentType("application/json;charset=utf-8");
		String s = new ObjectMapper().writeValueAsString(ResultVo.ok("退出成功"));
		response.getWriter().println(s);
	}

}
